
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.ChildrenMixin = ChildrenMixin;
exports.ParentMixin = ParentMixin;

const _vue = _interopRequireDefault(require('vue'));

const _vnodes = require('../utils/vnodes');

function ChildrenMixin(_parent, options) {
  let _inject; let
    _computed;

  if (options === void 0) {
    options = {};
  }

  const indexKey = options.indexKey || 'index';
  return _vue.default.extend({
    inject: (_inject = {}, _inject[_parent] = {
      default: null,
    }, _inject),
    computed: (_computed = {
      parent: function parent() {
        if (this.disableBindRelation) {
          return null;
        }

        return this[_parent];
      },
    }, _computed[indexKey] = function () {
      this.bindRelation();

      if (this.parent) {
        return this.parent.children.indexOf(this);
      }

      return null;
    }, _computed),
    mounted: function mounted() {
      this.bindRelation();
    },
    beforeDestroy: function beforeDestroy() {
      const _this = this;

      if (this.parent) {
        this.parent.children = this.parent.children.filter((item) => item !== _this);
      }
    },
    methods: {
      bindRelation: function bindRelation() {
        if (!this.parent || this.parent.children.indexOf(this) !== -1) {
          return;
        }

        const children = [].concat(this.parent.children, [this]);
        (0, _vnodes.sortChildren)(children, this.parent);
        this.parent.children = children;
      },
    },
  });
}

function ParentMixin(parent) {
  return {
    provide: function provide() {
      let _ref;

      return _ref = {}, _ref[parent] = this, _ref;
    },
    data: function data() {
      return {
        children: [],
      };
    },
  };
}
